<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card class="cardareas">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入商品名称" v-model="query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="checkGoodsBtn"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoodsBtn">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品列表区域 -->
      <el-table :data="goodsList" stripe border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格/元" width="90px"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量/g" width="90px"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑商品"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="editGoodsBtn(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除商品"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="deleteGoodsBtn(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[8, 15, 20, 25]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑商品列表对话框 -->
    <el-dialog title="编辑商品" :visible.sync="editGoodsVisible" width="50%">
      <el-form
        :model="editGoodsForm"
        :rules="editGoodsRules"
        ref="editGoodsRef"
        label-width="100px"
      >
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="editGoodsForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goodsPrice">
          <el-input v-model="editGoodsForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goodsweight">
          <el-input v-model="editGoodsForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editGoodsVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsBtnInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
    
<script>
export default {
  data() {
    return {
      goodsList: [],
      goodsInfo: {},
      total: 0,
      query: '',
      pagenum: 1,
      pagesize: 8,
      editGoodsVisible: false,
      editGoodsForm: {},
      editGoodsRules: {
        goods_name: [
          { required: true, message: '请输入商品名称！', trigger: 'blur' },
        ],
        goods_price: [
          { required: true, message: '请输入商品价格！', trigger: 'blur' },
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量！', trigger: 'blur' },
        ],
      },
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        },
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 页数发生变化时触发的事件
    handleSizeChange(newSize) {
      this.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页发生变化触发的事件
    handleCurrentChange(newPage) {
      this.pagenum = newPage
      this.getGoodsList()
    },
    // 查找商品按钮
    checkGoodsBtn() {
      this.getGoodsList()
    },
    // 添加商品按钮
    addGoodsBtn() {
      this.$router.push('/goods/add')
    },
    // 编辑商品按钮
    async editGoodsBtn(goods_id) {
      // 发送请求获取原始商品数据
      const { data: res } = await this.$http.get(`goods/${goods_id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.editGoodsForm = res.data
      this.editGoodsVisible = true
    },
    // 点击编辑商品的确定按钮
    async editGoodsBtnInfo() {
      const { data: res } = await this.$http.put(
        `goods/${this.editGoodsForm.goods_id}`
      )
      console.log(res)
      if (res.meta.status !== 201) return this.$message.error('没有接口怎么写？')
      this.$message.success(res.meta.msg)
      this.getGoodsList()
    },

    // 删除商品按钮
    async deleteGoodsBtn(goods_id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') return this.$message.info('已取消删除！')
      // 发送删除商品的请求
      const { data: res } = await this.$http.delete(`goods/${goods_id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getGoodsList()
    },
  },
}
</script>

<style lang="less" scoped>
</style>